package com.xuecheng.content.controller;


import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.TeachplanMediaDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.api.media.model.vo.BindTeachplanMediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
  *课程基本信息 前端控制器
  *@author itcast
 */
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    TeachplanService teachplanService;

    @RequestMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params,
                                  @RequestBody QueryCourseModel model) {
        //获取访问令牌中解析出的教育机构的信息id数据
        Long companyId = SecurityUtil.getCompanyId();
        //调用service
        PageVO<CourseBaseDTO> dtos = courseBaseService.queryCourseList(
                params, model, companyId);

        return dtos;
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        //1 将vo数据转化成dto数据
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);
        //2 获得教学机构的id值
        Long companyId = SecurityUtil.getCompanyId();
        dto.setCompanyId(companyId);
        //3 调用业务层获得结果数据
        CourseBaseDTO dtos = courseBaseService.createCourseBase(dto);
        return dtos;
    }

    @RequestMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO vo) {

        // 1.将vo数据转为dto数据
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.vo2dto(vo);

        // 2.获得教学机构的id值
        Long companyId = SecurityUtil.getCompanyId();

        dto.setCompanyId(companyId);

        // 3.调用service层获得结果数据
        CourseBaseDTO result = courseBaseService.modifyCourseBase(dto);

        return result;
    }

    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        return courseBaseService.getByCourseId(courseBaseId, companyId);
    }

    @DeleteMapping("course/{courseBaseId}")
    public void deleteCoursebase(@PathVariable Long courseBaseId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.deleteCourseBase(courseBaseId, companyId);
    }

    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }


    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();

        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());


        TeachplanMediaDTO resultDTO = teachplanService.associateMedia(teachplanMediaDTO, companyId);

        return resultDTO;
    }
}
